<template>
    <el-dialog :title="apply_name" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" draggable overflow>
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="setShop" v-auth="'applyshop.add'" v-if="!type">设置品名</el-button>
                </div>
                <div class="right-panel">
                    <div class="right-panel-search">
                        <el-input v-model="search.keyword" placeholder="关键词搜索" clearable @keyup.enter="upsearch"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" @click="unsetSearch">重置</el-button>
                    </div>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" @row-dblclick="selectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                    <el-table-column type="selection" width="50" reserve-selection align="center" v-if="!type"></el-table-column>
                    <el-table-column type="index" label="" fixed align="center" width="50"></el-table-column>
                    <el-table-column label="品名" prop="goods_name">
                        <template #default="scope">
                            <el-link :underline="false" type="primary" style="font-size:12px" @click="table_show(scope.row)">{{ scope.row.goods_name }}</el-link>
                        </template>
                    </el-table-column>
					<el-table-column label="货号" prop="goods_code"></el-table-column>
                    <el-table-column label="操作" align="center" width="170" v-if="!type">
						<template #default="scope">
							<div class="buttonFlex">
								<el-button type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'applyshop.edit'">编辑</el-button>
								<el-popconfirm title="确定删除吗?" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button type="danger" size="small" v-auth="'applyshop.delete'">删除</el-button>
									</template>
								</el-popconfirm>
							</div>
						</template>
					</el-table-column>
                </scTable>
            </el-main>
        </el-container>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
        </template>
        <set-shop v-if="dialog.shop" ref="shopDialog" @success="handleSuccess" @closed="dialog.shop = false"></set-shop>
    </el-dialog>
</template>

<script>
import SetShop from './setShop.vue';

export default {
  components: {  SetShop },
    props: {
        query: {type: Object, default: () => ({})},
    },
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '品名管理',
                edit: '品名管理',
                show: '查看'
            },
            visible: false,
            isSaveing: false,
            apiObj: this.$API.basic.shopApply.list,
            selection: [],
            search: {
                keyword: null,
            },
            dialog: {
                shop: false,
            },
            apply_id: "",
            apply_name: "",
            type: "",
        }
    },
    mounted() {
    },
    methods: {
        // 显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        // 表单注入数据
        setData(data,type) {
            this.apply_id = data.apply_id
            this.apply_name = data.apply_name
            this.type = type
        },
        // 选择事件
        selectionChange(selection) {
            if (this.type) {
                if (selection.length > 1) {
                    const del_row = selection.shift()
                    this.$refs.table.toggleRowSelection(del_row, false) //设置这一行取消选中
                }
                this.$emit('success', selection, this.type)
                this.visible = false;
            }
            this.selection = selection;
        },
        // 选中返回
        back() {
            this.$emit('success', this.selection, this.type)
            this.visible = false;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        setShop(){
            let form = {
                apply_id: this.apply_id,
                apply_name: this.apply_name
            }
            this.dialog.shop = true
			this.$nextTick(() => {
				this.$refs.shopDialog.open('add').getName(form)
			})
        },
        handleSuccess(){
            this.$refs.table.refresh()
        },
        table_show(row) {
			this.dialog.shop = true
			this.$nextTick(() => {
				this.$refs.shopDialog.open('show').setData(row)
			})
		},
        // 编辑
        table_edit(row) {
			this.dialog.shop = true
			this.$nextTick(() => {
				this.$refs.shopDialog.open('edit').setData(row)
			})
		},
        async table_del(row){
            var reqData = { apply_id: row.apply_id,goods_id:row.goods_id }
			var res = await this.$API.basic.shopApply.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, '提醒', { type: 'error' })
			}
        },
    }
}
</script>

<style></style>
